<template>
  <div>
    <Form :label-width="100" ref="formInline" :model="formInline">
      <Row class="query-form">
        <i-col span="6">
          <FormItem label="所属单位：">
            <cascaderDom ref="queryOrgCascader" @setOrgInfo="setOrgId"
                         :setCurrentName="{value: formInline.companyId,label : formInline.companyName}"
                         :rootOrgId="formInline.companyId" style="width: 100%"
                         :orgTree="true"></cascaderDom>
          </FormItem>
        </i-col>
        <i-col span="6">
          <FormItem label="开始月份：">
            <DatePicker type="month" placeholder="请选择开始月份" style="width: 100%"
                        v-model="formInline.startDate"></DatePicker>
          </FormItem>
        </i-col>
        <i-col span="6">
          <FormItem label="结束月份：">
            <DatePicker type="month" placeholder="请选择结束月份" style="width: 100%"
                        v-model="formInline.endDate"></DatePicker>
          </FormItem>
        </i-col>
        <i-col span="6">
          <div style="float: right;">
            <Button class="ivu-btn-query" style="margin-left: 10px" :loading="loading2" @click="exportExcel(2)">
              环保报告
            </Button>
            <Button class="ivu-btn-query" style="margin-left: 10px" :loading="loading1" @click="exportExcel(1)">
              安全报告
            </Button>
          </div>
        </i-col>
      </Row>
    </Form>
    <div style="height: calc(100vh - 180px)">
      <iframe ref="iframe" frameborder="0" align="middle" width="100%" height="100%"
              v-if="filePdf" :name="fileName"   :src=filePdf style="margin-top:10px;text-align:center;height: 100%;"></iframe>
    </div>

  </div>
</template>

<script>
import {Api} from '../../../api'

let cascaderDom = () => import('@/components/cascader-dom.vue');
import {sessionStorage} from '../../../common/cacheUtils.js';

export default {
  name: "safetyComprehensiveExport",
  components: {cascaderDom},
  data() {
    return {
      filePdf: '',
      fileName:'',
      tableHeight: '',
      loading1: false,
      loading2: false,
      formInline: {
        companyId: sessionStorage('userId').getObject('userId').companyId,
        companyName: sessionStorage('userId').getObject('userId').companyName
      },
    }
  },
  mounted() {
    this.tableHeight = window.innerHeight - 145;
  },
  methods: {
    exportExcel(type) {
      if (this.formInline.startDate == undefined || this.formInline.startDate == '') {
        this.$Message.error("请选择开始时间！");
        return;
      }
      if (this.formInline.endDate == undefined || this.formInline.endDate == '') {
        this.$Message.error("请选择结束时间！");
        return;
      }
      if (this.formInline.companyId == undefined || this.formInline.companyId == '') {
        this.$Message.error("请选择所属单位！");
        return;
      }
      this['loading' + type] = true
      let data = {
        orgName: this.formInline.companyName,
        orgId: this.formInline.companyId,
        startDate: this.formInline.startDate != undefined && this.formInline.startDate != '' ? this.formatDate(this.formInline.startDate, 'yyyy-MM') : '',
        endDate: this.formInline.endDate != undefined && this.formInline.endDate != '' ? this.formatDate(this.formInline.endDate, 'yyyy-MM') : '',
      };
      let url = Api.exportSafetyComprehensive
      this.fileName = '安全业务汇总报告.pdf'
      if (type == 2) {
        url = Api.exportEnComprehensive
        this.fileName = '环境保护管理工作总结.pdf'
      }
      this.filePdf = "./pdf/web/viewer.html?file=" + encodeURIComponent(url + "?startDate=" + data.startDate
      + "&endDate=" + data.endDate + "&orgId=" + data.orgId + "&filename="+ data.orgName +
          data.startDate + "_" + data.endDate + "_" + this.fileName)
      this['loading' + type] = false;
      /*
      this.$http.get(url, {
        params: data,
        responseType: 'blob'      //告诉服务器我们需要的响应格式
      }).then((res) => {
        this['loading' + type] = false

        let aLink = this.$refs.iframe
        // const aLink = document.createElement("a");
        let blob = new Blob([res.data], {
          type: res.bodyBlob.type      //将会被放入到blob中的数组内容的MIME类型
        });
        this.filePdf = "./pdf/web/viewer.html?file=" +  "#123wqd"
        console.log(this.filePdf)
        // aLink.download = fileName //标题
        // aLink.click()
        // document.body.appendChild(aLink)
      }, function (res) {
        this['loading' + type] = false
        this.$Message.error('生成失败');
      }) */
    },
    setOrgId(data) {
      console.log(data);
      this.formInline.companyId = data[data.length - 1];
    },
    formatDate(date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      };
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + '';
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
        }
      }
      return fmt;
    },
    padLeftZero(str) {
      return ('00' + str).substr(str.length);
    },
  }
}
</script>

<style scoped>

</style>
